package com.xywh.composeLibrary.view

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 *
 * @param label String
 * @param hint String 提示
 * @param start Dp 左边距
 * @param top Dp 上边距
 * @param end Dp 右边距
 * @param bottom Dp 底边距
 * @param height Dp 高度
 * @param maxLines Int 最大行数
 * @param enabled Boolean 是否可以
 * @param readOnly Boolean 是否可读
 * @param roundedCornerShape Dp 圆角大小
 * @param onTextChange Function1<String, Unit> 输入框文字变动回调
 * @param background Color 输入框背景
 * @param textColor Color 字体颜色
 * @param textSize TextUnit 字体大小
 * @param keyboardOptions KeyboardOptions 键盘配置
 * @param keyboardActions KeyboardActions 键盘回调
 * @param trailingIcon ImageVector 右侧图标
 * @param trailingIconTint Color 右侧图标颜色
 * @param leadingIcon ImageVector 左侧图标
 * @param leadingIconTint Color 左侧图标颜色
 * @param visualTransformation VisualTransformation  文本展示的转换 密码
 * @param cursorColor Color 光标颜色
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun XxTextField(
    label: @Composable (() -> Unit)? = null,
    hint: String,
    defaultTextValue: String = "",
    start: Dp = 0.dp,
    top: Dp = 0.dp,
    end: Dp = 0.dp,
    bottom: Dp = 0.dp,
    height: Dp = 56.dp,
    maxLines: Int = 1,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    roundedCornerShape: Dp = 10.dp,
    onTextChange: (String) -> Unit,
    background: Color = Color(0xFFF6F6F6),
    textColor: Color = Color.Gray,
    textSize: TextUnit = 14.sp,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    trailingIcon: ImageVector = Icons.Filled.Clear,
    trailingIconTint: Color = Color.Black,
    leadingIcon: ImageVector = Icons.Filled.AccountBox,
    leadingIconTint: Color = Color.Black,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    cursorColor: Color = MaterialTheme.colorScheme.primary
) {

    var textValue by remember { mutableStateOf(defaultTextValue) }

    TextField(

        modifier = Modifier
            .padding(top = top, start = start, end = end, bottom = bottom)
            .height(height)
            .fillMaxWidth(),
        shape = RoundedCornerShape(roundedCornerShape),
        value = textValue,
        visualTransformation = visualTransformation,
        onValueChange = {
            textValue = it
            onTextChange.invoke(it)
        },
        enabled = enabled,
        readOnly = readOnly,
        maxLines = maxLines,
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions,
        label = label,
        textStyle = TextStyle(
            fontSize = textSize,
        ),
        placeholder = {
            Text(hint, fontSize = textSize, color = Color.Gray)
        },
        trailingIcon = {
            if (textValue.isNotEmpty()) {
                IconButton(
                    onClick = {
                        textValue = ""
                        onTextChange.invoke(textValue)
                    },
                ) {
                    Icon(
                        imageVector = trailingIcon,
                        modifier = Modifier.size(14.dp),
                        contentDescription = "右侧图标",
                        tint = trailingIconTint
                    )
                }
            }

        },
        leadingIcon = {
            Icon(
                leadingIcon,
                contentDescription = "左侧图标",
                tint = leadingIconTint
            )

        },
        singleLine = true,
        colors = TextFieldDefaults.colors(
            unfocusedTextColor = textColor,
            disabledTextColor = Color.Transparent,
            unfocusedContainerColor = background,
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor = Color.Transparent,
            cursorColor = cursorColor,
        )
    )

}